﻿using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using Git;
using RequestDirectory;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Data
{
    static class RequserDirectoryDataOrderBy
    {
        /// <summary>
        /// 排序保存List
        /// </summary>
        public static List<TreeListNode> listorderby = new List<TreeListNode>();

        /// <summary>
        /// 排序
        /// </summary>
        public static void NodeOrderBys(TreeListNodes tl)
        {
            listorderby.Clear();
            NodeOrderBy(tl);
        }

        /// <summary>
        /// 排序
        /// </summary>
        private static void NodeOrderBy(TreeListNodes tl)
        {
            foreach (TreeListNode Nodee in tl)
            {
                if (Nodee.GetValue("ID") != null)
                {
                    listorderby.Add(Nodee);
                    if (Nodee.Nodes.Count > 0)
                    {
                        NodeOrderBy(Nodee.Nodes);
                    }
                }
            }
        }


    }

    static class RequserDirectoryDataControl
    {
        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="type"></param>
        public static void SetAppendNode(TreeList tl, TreeListNode tln, String type)
        {
            if (tln != null)
            {
                //文件类型不能添加子级
                string stringtype = (string)tln.GetValue("type");
                if (stringtype.Equals("file"))
                {
                    return;
                }
            }
            if (tln != null)
            {
                tl.AppendNode(new object[] { "新建标签", type, 0, GUID.getGUID(), "" }, tln);
                tln.ExpandAll();
            }
            else
            {
                List<RequserDirectoryData> NodeDatalist = (List<RequserDirectoryData>)tl.DataSource;
                if (NodeDatalist == null)
                {
                    NodeDatalist = new  List<RequserDirectoryData>();
                    tl.DataSource = NodeDatalist;
                }
                NodeDatalist.Add(
                    new RequserDirectoryData()
                    {
                        key = "新建标签",
                        type = type,
                        opendnumber = 0,
                        ID = GUID.getGUID(),
                        ParentID = "",
                    });
                tl.RefreshDataSource();
            }
            NodeIco.SetUpStateImage(tl);
            SetSave(tl);
        }

        /// <summary>
        /// 保存节点
        /// </summary>
        public static void SetSave(TreeList tl)
        {
            //RDTool.listorderby.Clear();
            //NodeOrderBy(tl.Nodes);
            RequserDirectoryDataOrderBy.NodeOrderBys(tl.Nodes);
            List<TreeListNode> listnode = RequserDirectoryDataOrderBy.listorderby;
            List<RequserDirectoryData> listorderby = new List<RequserDirectoryData>();
            foreach (TreeListNode Nodee in listnode)
            {
                listorderby.Add(new RequserDirectoryData()
                {
                    ID = Nodee.GetValue("ID").ToString(),
                    key = Nodee.GetValue("key") != null ? Nodee.GetValue("key").ToString() : "",
                    type = Nodee.GetValue("type") != null ? Nodee.GetValue("type").ToString() : "",
                    ParentID = Nodee.GetValue("ParentID") != null ? Nodee.GetValue("ParentID").ToString() : "",
                    opendnumber = Nodee.GetValue("opendnumber") != null ? (int)Nodee.GetValue("opendnumber") : 0,
                });
            }

            string thisExeDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string thisDirectory = thisExeDirectory + GlobalDirectory.SaveDirectory;
            if (!Directory.Exists(thisDirectory))
            {
                Directory.CreateDirectory(thisDirectory);
            }

            //保存节点
            string thisFile = thisDirectory + @"\\ItemNode.json";
            using (StreamWriter file = File.CreateText(thisFile))
            {
                Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
                serializer.Serialize(file, listorderby);
            }
        }

        /// <summary>
        /// 删除节点,包含文件
        /// </summary>
        public static void SetDelete(TreeList tl, TreeListNode tn)
        {
            string stringtypef = (string)tn.GetValue("type");
            string stringIDf = (string)tn.GetValue("ID");
            if (stringtypef.Equals("file"))
            {
                string thisExeFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                string thisDirectory = thisExeFile + GlobalDirectory.SaveDirectory + "\\JSONDataList\\" + stringIDf;
                if (Directory.Exists(thisDirectory))
                {
                    DirectoryInfo di = new DirectoryInfo(thisDirectory);
                    di.Delete(true);
                }
                else {
                    tl.DeleteNode(tn);
                }
            }
            else
            {
                tl.DeleteNode(tn);
            }
        }

        /// <summary>
        /// 复制节点
        /// </summary>
        /// <param name="tl"></param>
        /// <param name="tln"></param>
        public static string SetCopy(TreeList tl, TreeListNode tln)
        {
            string guid = GUID.getGUID();
            TreeListNode enode = tl.AppendNode(new object[] { "新复制:" + tln.GetValue("key").ToString(), "file", 0, guid, "" }, tln);
            tl.MoveNode(enode, tln.ParentNode, true);
            TreeListNodes max = tln.ParentNode.Nodes;
            int i = 0;
            foreach (TreeListNode min in max)
            {
                if (min.Equals(tln))
                {
                    break;
                }
                i++;
            }
            tl.SetNodeIndex(enode, i + 1);
            NodeIco.SetUpStateImage(tl);
            SetSave(tl);
            return guid;
        }

    }
}
